/* File: lab3_steg0.c
* ------------------
* Start file for Assignment #3, PTek, autumn 2014.
* Author: CES
* Last modified: 2014-12-16
*/

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "genlib.h"
#include "simpio.h"
#include "strlib.h"

/*
* Constant: QUIT
* --------------
* The constant QUIT is to break the program
* from the main menu.
*/

#define QUIT -1

/*
* Constant: INITIAL_SIZE
* ----------------------
* The constant defines the initial
* size of the dynamic array holding
* the cat data.
*/

#define INITIAL_SIZE 10

/* Constant: MAX_COLOURS
* ---------------------
* The constant defines the maximum number
* of colours that one cat can have.
*/

#define MAX_COLOURS 5

/* Type definition: catT
* ---------------------
* The catT is a simple struct containing all the
* attributes of the cats. The colours attribute
* is an array, since a cat can have several colours.
*/

typedef struct {
	int id;
	string name;
	char gender;
	int birthYear;
	int numberOfColours;
	string colours[MAX_COLOURS];
} catT;

/* Type definition: DBT
* --------------------
* The DBT type is a pointer to a struct containing a dynamic
* array of catT and three integer counters: one to keep track of the
* current size of the catT vector, one to keep track of the
* current number of cats in the database and another to keep track
* of the next available id number.
*/

typedef struct{
	catT *cats;
	int currentMaxSize;
	int currentNumberOfCats;
	int nextId;
} *DBT;

DBT initializedatabase(void){
	DBT database;

	database = New(DBT);
	database->currentNumberOfCats = 0;
	database->currentMaxSize = INITIAL_SIZE;
	database->nextId = 1;
	database->cats = (catT*)malloc(INITIAL_SIZE * sizeof(catT));
	return database;
}

void printIntroMessage(void){
	printf("This program helps organize information about cats and their attributes.\nYou may add and remove cats and their attributes,\nlist the names of cats currently in the organizer,\nlist the cats and their attributes,\nlook up the attributes of an individual cat,\nget statistics from the register,\nor sort the register.\n\n\n\n\n");
}

string getRegisterFileName(void){

	string fileName;
	return fileName = GetLine();

}

void loaddatabase(string fileName, DBT database){

	FILE* inFile;
	FILE* outFile;
	int i, x;


	inFile = fopen(fileName, "r");

	if (!inFile){
		outFile = fopen(fileName, "w");
	}
	else{
		database->currentNumberOfCats = StringToInteger(ReadLine(inFile));
		database->nextId = database->currentNumberOfCats + 1;
		if (database->currentNumberOfCats > INITIAL_SIZE)database->currentMaxSize = database->currentNumberOfCats;
		else database->currentMaxSize = INITIAL_SIZE;
		for (i = 0; i < database->currentNumberOfCats; i++)
		{
			database->cats[i].id = StringToInteger(ReadLine(inFile));
			database->cats[i].name = ReadLine(inFile);
			database->cats[i].gender = IthChar(ReadLine(inFile), 0);
			database->cats[i].birthYear = StringToInteger(ReadLine(inFile));
			database->cats[i].numberOfColours = StringToInteger(ReadLine(inFile));
			x = 0;
			while (x < database->cats[i].numberOfColours)
			{
				database->cats[i].colours[x] = ReadLine(inFile);
				x++;
			}
		}
	}

}

void main(void){
	DBT database;
	string fileName;
	database = initializedatabase();
	printIntroMessage();
	fileName = getRegisterFileName();
	loaddatabase(fileName, database);
	//executeCommands(database);
	//savedatabase(fileName, database);
}

